package com.lxk.ums.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.ums.controller.app.vo.AppAddressParam;
import com.lxk.ums.controller.app.vo.AppAddressUpdateParam;
import com.lxk.ums.controller.app.vo.AppDefaultParam;
import com.lxk.ums.domain.vo.UmsUserAddressVo;
import com.lxk.ums.service.IUmsUserAddressService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * app用户收货地址
 *
 * @author lxk
 * @date 2023-09-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/ums/userAddress")
public class AppUserAddressController {

    private final IUmsUserAddressService iUmsUserAddressService;

    /**
     * 添加收货地址
     *
     * @param appAddressParam
     * @return
     */
    @Log(title = "app用户收货地址", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/add/address")
    public R<Boolean> addAddress(@Validated @RequestBody AppAddressParam appAddressParam) {
        return R.ok(iUmsUserAddressService.addAddress(appAddressParam));
    }

    /**
     * 修改收货地址
     *
     * @param appAddressUpdateParam
     */
    @Log(title = "app用户收货地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/update/address")
    public R<Boolean> updateAddress(@Validated @RequestBody AppAddressUpdateParam appAddressUpdateParam) {
        return R.ok(iUmsUserAddressService.updateAddress(appAddressUpdateParam));
    }

    /**
     * 修改默认
     *
     * @param appDefaultParam
     */
    @Log(title = "app用户收货地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/update/default")
    public R<Boolean> updateDefault(@Validated @RequestBody AppDefaultParam appDefaultParam) {
        return R.ok(iUmsUserAddressService.updateDefault(appDefaultParam));
    }

    /**
     * 地址列表
     * @param pageQuery
     */
    @GetMapping("/list")
    public TableDataInfo<UmsUserAddressVo> list(PageQuery pageQuery){
        pageQuery.setIsAsc("desc");
        pageQuery.setOrderByColumn("defaultStatus");
        Page<UmsUserAddressVo> page = iUmsUserAddressService.queryByPageList(pageQuery);
        return TableDataInfo.build(page);
    }

    /**
     * 地址详细信息
     * @param addressId
     */
    @GetMapping("/address/details/{addressId}")
    public R<UmsUserAddressVo> addressDetails(@PathVariable Long addressId){
        return R.ok(iUmsUserAddressService.queryById(addressId));
    }

    /**
     * 删除地址
     * @param addressId 地址id
     * @return
     */
    @DeleteMapping("/delete/{addressId}")
    public R<String> deleteAddress(@PathVariable Long addressId){
        if (iUmsUserAddressService.deleteAddressById(addressId)){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
